﻿using System;
//===============================================================================
// Apolineo Patterns & Practices LeanEngine Framework
//===============================================================================
// Copyright © Apolineo Desenvolvimento de Sistemas Ltda.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System.Windows.Forms;
using LeanEngine.SalesConcept.Entity;
using LeanEngine.Entity;
using LeanEngine.Extensions;
using System.ComponentModel;

namespace LeanEngine.SalesConcept.Windows.Control
{
    public partial class MaintenanceControlOfProduct : UserControl, IMaintenanceControl<Product>
    {
        #region Constructor Members

        public MaintenanceControlOfProduct()
        {
            InitializeComponent();

            this.txtYearProduction.Text = DateTime.Now.Year.ToString();
            this.txtQuantityInStock.Text = "0";
        }

        #endregion

        #region Property Members

        private Product CurrentProduct { get; set; }

        //Através do controle de integração dos validadores com controles de tela
        //PropertyValidatorProvider (LeanEngine.Validation.Windows.Forms) o lob se encarregra de
        //executar os validadores passando os valores do controle e retornando
        //na propriedade ErrorMessages as mensagens dos erros (caso exista)
        [Browsable(false)]
        public string ErrorMessages
        {
            get { return this.propertyValidatorProvider1.ErrorMessages; }
        }

        #endregion

        #region IMaintenanceControl<Product> Members

        public Product GetControlsValues()
        {
            if (this.CurrentProduct.IsNull()) this.CurrentProduct = new Product();

            this.CurrentProduct.Name = this.txtName.Text.Trim();
            this.CurrentProduct.Color = this.txtColor.Text.Trim();
            if (txtQuantityInStock.Text.IsNumeric()) this.CurrentProduct.QuantityInStock = this.txtQuantityInStock.Text.ToInt32();
            if (this.txtYearProduction.Text.IsNumeric()) this.CurrentProduct.YearProduction = this.txtYearProduction.Text.ToInt32();

            return this.CurrentProduct;
        }

        public void SetControlsValues(Product value)
        {
            this.CurrentProduct = value;

            this.txtName.Text = this.CurrentProduct.Name;
            this.txtColor.Text = this.CurrentProduct.Color;
            this.txtQuantityInStock.Text = this.CurrentProduct.QuantityInStock.ToString();
            this.txtYearProduction.Text = this.CurrentProduct.YearProduction.ToString();
        }

        public void ClearControls()
        {
            this.txtName.Text = string.Empty;
            this.txtColor.Text = string.Empty;
            this.txtQuantityInStock.Text = "0";
            this.txtYearProduction.Text = DateTime.Now.Year.ToString();

            this.txtName.Focus();
        }

        #endregion
    }
}
